home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / PowerPlant / 3D Additions 1.7 / 3D Additions / 3DPanes.cp < prev    next >
Encoding:
Text File  |  1995-10-24  |  19.1 KB  |  643 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. // 3DPanes.cp          ©1995 J. Rodden, DD/MF & Associates. All rights reserved
  3. // ===========================================================================
  4. // Provides 3D panes (raised and inset) as proposed in Develop #15.
  5. //
  6. // There are two types of 3DPanes: panels, in which the whole pain is drawn
  7. // into, and frames, which only draw a box around the pane.
  8. //
  9. // There are also two implementations of each type of pane. The first uses the
  10. // colors stored via 3DUtilities so that all 3DPane colors can be controlled
  11. // in unison at runtime. The second is entirely resource based and gets all its 
  12. // color information from a Constructor template resource.
  13. //
  14. // This source code is loosely based on and heavily inspired by source code
  15. // by James W. Osborne, copyright (c) 1993, Apple Computer.
  16.  
  17. #include "3DPanes.h"
  18.  
  19. #include "3DAttachments.h"
  20. #include "3DDrawingUtils.h"
  21.  
  22. #include <LStream.h>
  23. #include <PP_Types.h>
  24. #include <UDrawingUtils.h>
  25. #include <URegistrar.h>
  26.  
  27. // local (static) prototype
  28.  
  29. static RGBColor    ByteToGray( Byte inByte);
  30.  
  31.  
  32. // ===========================================================================
  33. // • C3DPanel                                                        C3DPanel •
  34. // ===========================================================================
  35.  
  36. // ---------------------------------------------------------------------------
  37. //    • RegisterSelf [static]       (a self-registering class...what a novel concept)
  38. // ---------------------------------------------------------------------------
  39.  
  40. void
  41. C3DPanel::RegisterSelf()
  42. {
  43.     URegistrar::RegisterClass(class_ID,CreateFromStream);
  44. }
  45.  
  46.  
  47. // ---------------------------------------------------------------------------
  48. //        • CreateFromStream [static]
  49. // ---------------------------------------------------------------------------
  50. //    This is the function you register with URegistrar to create a C3DPanel
  51. //    pane from a resource
  52.  
  53. C3DPanel*
  54. C3DPanel::CreateFromStream(
  55.     LStream *inStream)
  56. {
  57.     return (new C3DPanel(inStream));
  58. }
  59.  
  60. // ---------------------------------------------------------------------------
  61. //        • C3DPanel
  62. // ---------------------------------------------------------------------------
  63. //    The default constructor.
  64.  
  65. C3DPanel::C3DPanel()
  66.     : LPane(), mIsInset(false)
  67. {
  68. }
  69.  
  70. // ---------------------------------------------------------------------------
  71. //        • C3DPanel
  72. // ---------------------------------------------------------------------------
  73. //    Construct from data structures.
  74.  
  75. C3DPanel::C3DPanel(const SPaneInfo &inPaneInfo, const Boolean &inIsInset)
  76.     : LPane(inPaneInfo), mIsInset(inIsInset)
  77. {
  78. }
  79.  
  80. // ---------------------------------------------------------------------------
  81. //        • C3DPanel
  82. // ---------------------------------------------------------------------------
  83. //    The construct-from-stream constructor.
  84.  
  85. C3DPanel::C3DPanel(LStream *inStream)
  86.     : LPane(inStream)
  87. {
  88.     inStream->ReadData(&mIsInset, sizeof(Boolean));
  89. }
  90.  
  91. // ---------------------------------------------------------------------------
  92. //        • DrawSelf
  93. // ---------------------------------------------------------------------------
  94.  
  95. void
  96. C3DPanel::DrawSelf()
  97. {
  98.     Rect         theRect;
  99.     RGBColor    theColor;
  100.     
  101.     CalcLocalFrameRect(theRect);
  102.  
  103.     St3DDeviceLoop    theLoop(theRect);
  104.     
  105.     while (theLoop.Next()) {
  106.         if (theLoop.CurrentDeviceIs3DCapable()) {
  107.             if (mIsInset) {
  108.                 ::Draw3DInsetPanel(&theRect);
  109.                 ::Get3DLightColor(&theColor);
  110.             } else {
  111.                 ::Draw3DRaisedPanel(&theRect);
  112.                 ::Get3DBackColor(&theColor);
  113.             }
  114.             ::RGBBackColor(&theColor);    // set background color for text captions
  115.         } else {
  116.             ::FrameRect(&theRect);
  117.         }
  118.     }
  119. }
  120.  
  121.  
  122.  
  123. // ===========================================================================
  124. // • C3DFrame                                                        C3DFrame •
  125. // ===========================================================================
  126.  
  127. // ---------------------------------------------------------------------------
  128. //    • RegisterSelf [static]       (a self-registering class...what a novel concept)
  129. // ---------------------------------------------------------------------------
  130.  
  131. void
  132. C3DFrame::RegisterSelf()
  133. {
  134.     URegistrar::RegisterClass(class_ID,CreateFromStream);
  135. }
  136.  
  137.  
  138. // ---------------------------------------------------------------------------
  139. //        • CreateFromStream [static]
  140. // ---------------------------------------------------------------------------
  141. //    This is the function you register with URegistrar to create a C3DFrame
  142. //    pane from a resource
  143.  
  144.  
  145. C3DFrame*
  146. C3DFrame::CreateFromStream(
  147.     LStream *inStream)
  148. {
  149.     return (new C3DFrame(inStream));
  150. }
  151.  
  152. // ---------------------------------------------------------------------------
  153. //        • C3DFrame
  154. // ---------------------------------------------------------------------------
  155. //    The default constructor.
  156.  
  157. C3DFrame::C3DFrame()
  158.     : LPane(), mIsInset(false)
  159. {
  160. }
  161.  
  162. // ---------------------------------------------------------------------------
  163. //        • C3DFrame
  164. // ---------------------------------------------------------------------------
  165. //    Construct from data structures.
  166.  
  167. C3DFrame::C3DFrame(const SPaneInfo &inPaneInfo, const Boolean &inIsInset)
  168.     : LPane(inPaneInfo), mIsInset(inIsInset)
  169. {
  170. }
  171.  
  172. // ---------------------------------------------------------------------------
  173. //        • C3DFrame
  174. // ---------------------------------------------------------------------------
  175. //    The construct-from-stream constructor.
  176.  
  177. C3DFrame::C3DFrame(LStream *inStream)
  178.     : LPane(inStream)
  179. {
  180.     inStream->ReadData(&mIsInset, sizeof(Boolean));
  181. }
  182.  
  183. // ---------------------------------------------------------------------------
  184. //        • DrawSelf
  185. // ---------------------------------------------------------------------------
  186.  
  187. void
  188. C3DFrame::DrawSelf()
  189. {
  190.     Rect         theRect;
  191.     RGBColor    theColor;
  192.     
  193.     CalcLocalFrameRect(theRect);
  194.  
  195.     St3DDeviceLoop    theLoop(theRect);
  196.     
  197.     while (theLoop.Next()) {
  198.         if (theLoop.CurrentDeviceIs3DCapable()) {
  199.             if (mIsInset) {
  200.                 ::Draw3DInsetFrame(&theRect);
  201.             } else {
  202.                 ::Draw3DRaisedFrame(&theRect);
  203.             }
  204.             ::Get3DBackColor(&theColor);
  205.             ::RGBBackColor(&theColor);    // set background color for text captions
  206.         } else {
  207.             ::FrameRect(&theRect);
  208.         }
  209.     }
  210. }
  211.  
  212.  
  213.  
  214. // ===========================================================================
  215. // • CThreeDPanel                                                CThreeDPanel •
  216. // ===========================================================================
  217.  
  218. // ===========================================================================
  219. //    • RegisterSelf [static]                   (a self-registering class...what a novel concept)
  220. // ===========================================================================
  221.  
  222. void
  223. CThreeDPanel::RegisterSelf()
  224. {
  225.     URegistrar::RegisterClass(class_ID,CreateFromStream);
  226. }
  227.  
  228.  
  229. // ---------------------------------------------------------------------------
  230. //        • CreateFromStream [static]
  231. // ---------------------------------------------------------------------------
  232. //    This is the function you register with URegistrar to create a CThreeDPanel
  233. //    pane from a resource
  234.  
  235. CThreeDPanel*
  236. CThreeDPanel::CreateFromStream(
  237.     LStream *inStream)
  238. {
  239.     return (new CThreeDPanel(inStream));
  240. }
  241.  
  242. // ---------------------------------------------------------------------------
  243. //        • CThreeDPanel
  244. // ---------------------------------------------------------------------------
  245. //    The default constructor.
  246.  
  247. CThreeDPanel::CThreeDPanel()
  248.     : LPane(),
  249.       mBackgroundColor(kBackgroundGray),
  250.       mUpperLeftColor(kWhite),
  251.       mLowerRightColor(kShadowGray),
  252.       mHasBox(false)
  253. {
  254. }
  255.  
  256. // ---------------------------------------------------------------------------
  257. //        • CThreeDPanel
  258. // ---------------------------------------------------------------------------
  259. //    Construct from data structures.
  260.  
  261. CThreeDPanel::CThreeDPanel(
  262.     const SPaneInfo &inPaneInfo,
  263.     const RGBColor& inBkgdColor,
  264.     const RGBColor& inULColor,
  265.     const RGBColor& inLRColor,
  266.     const Boolean inHasBox)
  267.     : LPane(inPaneInfo)
  268. {
  269.     SetColors( inBkgdColor, inULColor, inLRColor, inHasBox);
  270. }
  271.  
  272. // ---------------------------------------------------------------------------
  273. //        • CThreeDPanel
  274. // ---------------------------------------------------------------------------
  275. //    The construct-from-stream constructor.
  276.  
  277. CThreeDPanel::CThreeDPanel(LStream *inStream)
  278.     : LPane(inStream)
  279. {
  280.     inStream->ReadData(&mBackgroundColor, sizeof(RGBColor));
  281.     inStream->ReadData(&mUpperLeftColor, sizeof(RGBColor));
  282.     inStream->ReadData(&mLowerRightColor, sizeof(RGBColor));
  283.     inStream->ReadData(&mHasBox, sizeof(Boolean));
  284. }
  285.  
  286. // ---------------------------------------------------------------------------
  287. //        • SetColors
  288. // ---------------------------------------------------------------------------
  289.  
  290. void
  291. CThreeDPanel::SetColors(
  292.     const RGBColor&    inBkgdColor,
  293.     const RGBColor&    inULColor,
  294.     const RGBColor&    inLRColor,
  295.     const Boolean    inHasBox)
  296. {
  297.     mBackgroundColor    = inBkgdColor;
  298.     mUpperLeftColor        = inULColor;
  299.     mLowerRightColor    = inLRColor;
  300.     mHasBox                = inHasBox;
  301. }
  302.  
  303. // ---------------------------------------------------------------------------
  304. //        • GetColors
  305. // ---------------------------------------------------------------------------
  306.  
  307. void
  308. CThreeDPanel::GetColors(
  309.     RGBColor&    inBkgdColor,
  310.     RGBColor&    inULColor,
  311.     RGBColor&    inLRColor,
  312.     Boolean        inHasBox)
  313. {
  314.     inBkgdColor    = mBackgroundColor;
  315.     inULColor    = mUpperLeftColor;
  316.     inLRColor    = mLowerRightColor;
  317.     inHasBox    = mHasBox;
  318. }
  319.  
  320. // ---------------------------------------------------------------------------
  321. //        • DrawSelf
  322. // ---------------------------------------------------------------------------
  323.  
  324. void
  325. CThreeDPanel::DrawSelf()
  326. {
  327.     Rect         theRect;
  328.     
  329.     CalcLocalFrameRect(theRect);
  330.  
  331.     St3DDeviceLoop    theLoop(theRect);
  332.     
  333.     while (theLoop.Next()) {
  334.         if (theLoop.CurrentDeviceIs3DCapable()) {
  335.             ::Draw3DPanel( &theRect, &mBackgroundColor, &mUpperLeftColor,
  336.                                      &mLowerRightColor, mHasBox);
  337.             ::RGBBackColor(&mBackgroundColor);    // set background color for text captions
  338.         } else {
  339.             ::FrameRect(&theRect);
  340.         }
  341.     }
  342. }
  343.  
  344.  
  345.  
  346. // ===========================================================================
  347. // • CThreeDFrame                                                        CThreeDFrame •
  348. // ===========================================================================
  349.  
  350. // ---------------------------------------------------------------------------
  351. //    • RegisterSelf [static]       (a self-registering class...what a novel concept)
  352. // ---------------------------------------------------------------------------
  353.  
  354. void
  355. CThreeDFrame::RegisterSelf()
  356. {
  357.     URegistrar::RegisterClass(class_ID,CreateFromStream);
  358. }
  359.  
  360.  
  361. // ---------------------------------------------------------------------------
  362. //        • CreateFromStream [static]
  363. // ---------------------------------------------------------------------------
  364. //    This is the function you register with URegistrar to create a CThreeDFrame
  365. //    pane from a resource
  366.  
  367. CThreeDFrame*
  368. CThreeDFrame::CreateFromStream(
  369.     LStream *inStream)
  370. {
  371.     return (new CThreeDFrame(inStream));
  372. }
  373.  
  374. // ---------------------------------------------------------------------------
  375. //        • CThreeDFrame
  376. // ---------------------------------------------------------------------------
  377. //    The default constructor.
  378.  
  379. CThreeDFrame::CThreeDFrame()
  380.     : LPane(),
  381.       mUpperLeftColor(kShadowGray),
  382.       mLowerRightColor(kWhite)
  383. {
  384. }
  385.  
  386. // ---------------------------------------------------------------------------
  387. //        • CThreeDCThreeDFramePane
  388. // ---------------------------------------------------------------------------
  389. //    Construct from data structures.
  390.  
  391. CThreeDFrame::CThreeDFrame(
  392.     const SPaneInfo &inPaneInfo,
  393.     const RGBColor& inULColor,
  394.     const RGBColor& inLRColor)
  395.     : LPane(inPaneInfo)
  396. {
  397.     SetColors( inULColor, inLRColor);
  398. }
  399.  
  400. // ---------------------------------------------------------------------------
  401. //        • CThreeDFrame
  402. // ---------------------------------------------------------------------------
  403. //    The construct-from-stream constructor.
  404.  
  405. CThreeDFrame::CThreeDFrame(LStream *inStream)
  406.     : LPane(inStream)
  407. {
  408.     inStream->ReadData(&mUpperLeftColor, sizeof(RGBColor));
  409.     inStream->ReadData(&mLowerRightColor, sizeof(RGBColor));
  410. }
  411.  
  412. // ---------------------------------------------------------------------------
  413. //        • SetColors
  414. // ---------------------------------------------------------------------------
  415.  
  416. void
  417. CThreeDFrame::SetColors(
  418.     const RGBColor&    inULColor,
  419.     const RGBColor&    inLRColor)
  420. {
  421.     mUpperLeftColor        = inULColor;
  422.     mLowerRightColor    = inLRColor;
  423. }
  424.  
  425. // ---------------------------------------------------------------------------
  426. //        • GetColors
  427. // ---------------------------------------------------------------------------
  428.  
  429. void
  430. CThreeDFrame::GetColors(
  431.     RGBColor&    inULColor,
  432.     RGBColor&    inLRColor)
  433. {
  434.     inULColor = mUpperLeftColor;
  435.     inLRColor = mLowerRightColor;
  436. }
  437.  
  438. // ---------------------------------------------------------------------------
  439. //        • DrawSelf
  440. // ---------------------------------------------------------------------------
  441.  
  442. void
  443. CThreeDFrame::DrawSelf()
  444. {
  445.     Rect         theRect;
  446.     
  447.     CalcLocalFrameRect(theRect);
  448.  
  449.     U3DDrawingUtils::Draw3DFrame( &theRect, &mUpperLeftColor, &mLowerRightColor);
  450. }
  451.  
  452.  
  453.  
  454. // ===========================================================================
  455. // • Local Utilities                                         Local Utilities •
  456. // ===========================================================================
  457.  
  458. // ---------------------------------------------------------------------------
  459. //        • ByteToGray [static]
  460. // ---------------------------------------------------------------------------
  461. // Convert a single byte to one of the 16 basic grays.
  462.  
  463. static RGBColor ByteToGray( Byte inByte)
  464. {
  465.     Int16        tempVal = 0;
  466.     
  467.     tempVal = (inByte * 0x1000) + (inByte * 0x0100) + (inByte * 0x0010) + inByte;
  468.     
  469.     RGBColor    theGray = { tempVal, tempVal, tempVal};
  470.     
  471.     return theGray;
  472. }
  473.  
  474.  
  475.  
  476. // ===========================================================================
  477. // • C3DPanelT                                                       C3DPanelT •
  478. // ===========================================================================
  479.  
  480. // WARNING: The following is experimental and not currently supported.
  481. // ===========================================================================
  482. //    Once template support is available (in CW5), this templatized version may
  483. //    be better. With it you can typedef your own color styles and minimize
  484. //    data duplication. For instance:
  485. //
  486. //        typedef C3DPaneT<kBackgroundGray,kWhite,kShadowGray,false>    CThreeDRaisedPane;
  487. //        typedef C3DPaneT<kWhite,kShadowGray,kWhite,true>            CThreeDInsetPane;
  488. //        typedef C3DFrameT<kWhite,kShadowGray>                        CThreeDRaisedFrame;
  489. //        typedef C3DFrameT<kShadowGray,kWhite>                        CThreeDInsetFrame;
  490. //
  491. //    would give you raised and inset panes and frames respectively using the
  492. //    coloring scheme proposed in Develop #15. (For each of your own typedef'ed
  493. //    versions, be sure to use a distinct class id.)
  494. //
  495. //    NOTE: I haven't tested this version since I don't have access to a template compiler :)
  496.  
  497. //#ifdef __Templates
  498.  
  499. // ===========================================================================
  500. // • C3DPanelT                                                       C3DPanelT •
  501. // ===========================================================================
  502.  
  503. // ---------------------------------------------------------------------------
  504. //    • RegisterSelf [static]       (a self-registering class...what a novel concept)
  505. // ---------------------------------------------------------------------------
  506.  
  507. template <RGBColor TBKColor, RGBColor TULColor, RGBColor TLRColor, Boolean TFrameIt>
  508. void C3DPanelT<TBKColor,TULColor,TLRColor,TFrameIt>::RegisterSelf()
  509. {
  510.     URegistrar::RegisterClass(class_ID,CreateFromStream);
  511. }
  512.  
  513.  
  514. // ---------------------------------------------------------------------------
  515. //        • CreateFromStream [static]
  516. // ---------------------------------------------------------------------------
  517. //    This is the function you register with URegistrar to create a CToolbar
  518. //    window from a resource
  519.  
  520. template <RGBColor TBKColor, RGBColor TULColor, RGBColor TLRColor, Boolean TFrameIt>
  521. C3DPanelT<TBKColor,TULColor,TLRColor,TFrameIt>*
  522. C3DPanelT<TBKColor,TULColor,TLRColor,TFrameIt>::CreateFromStream(
  523.     LStream *inStream)
  524. {
  525.     return (new C3DPanelT<TBKColor,TULColor,TLRColor>(inStream));
  526. }
  527.  
  528. // ---------------------------------------------------------------------------
  529. //        • C3DPanelT
  530. // ---------------------------------------------------------------------------
  531. //    The default constructor does nothing.
  532.  
  533. template <RGBColor TBKColor, RGBColor TULColor, RGBColor TLRColor, Boolean TFrameIt>
  534. C3DPanelT<TBKColor,TULColor,TLRColor,TFrameIt>::C3DPanelT()
  535. {
  536. }
  537.  
  538. // ---------------------------------------------------------------------------
  539. //        • C3DPanelT
  540. // ---------------------------------------------------------------------------
  541. //    The construct-from-stream constructor just constructs the base class.
  542.  
  543. template <RGBColor TBKColor, RGBColor TULColor, RGBColor TLRColor, Boolean TFrameIt>
  544. C3DPanelT<TBKColor,TULColor,TLRColor,TFrameIt>::C3DPanelT(LStream *inStream)
  545.     : LPanel(inStream)
  546. {
  547. }
  548.  
  549. // ---------------------------------------------------------------------------
  550. //        • DrawSelf
  551. // ---------------------------------------------------------------------------
  552.  
  553. template <RGBColor TBKColor, RGBColor TULColor, RGBColor TLRColor, Boolean TFrameIt>
  554. void
  555. C3DPanelT<TBKColor,TULColor,TLRColor,TFrameIt>::DrawSelf()
  556. {
  557.     Rect         theRect;
  558.     
  559.     CalcPortFrameRect(theRect);
  560.  
  561.     St3DDeviceLoop    theLoop(theRect);
  562.     
  563.     while (theLoop.Next()) {
  564.         if (theLoop.CurrentDeviceIs3DCapable()) {
  565.             ::Draw3DPanel( &theRect, &TBKColor, &TULColor, &TLRColor, TFrameIt);
  566.     
  567.             ::RGBBackColor(&TBKColor);    // set background color for text captions
  568.         } else {
  569.             ::FrameRect(&theRect);
  570.         }
  571.     }
  572. }
  573.  
  574.  
  575.  
  576. // ===========================================================================
  577. // • C3DFrameT                                                       C3DFrameT •
  578. // ===========================================================================
  579.  
  580. // ---------------------------------------------------------------------------
  581. //    • RegisterSelf [static]       (a self-registering class...what a novel concept)
  582. // ---------------------------------------------------------------------------
  583.  
  584. template <RGBColor TULColor, RGBColor TLRColor>
  585. void C3DFrameT<TULColor,TLRColor>::RegisterSelf()
  586. {
  587.     URegistrar::RegisterClass(class_ID,CreateFromStream);
  588. }
  589.  
  590.  
  591. // ---------------------------------------------------------------------------
  592. //        • CreateFromStream [static]
  593. // ---------------------------------------------------------------------------
  594. //    This is the function you register with URegistrar to create a C3DFrame
  595. //    pane from a resource
  596.  
  597.  
  598. template <RGBColor TULColor, RGBColor TLRColor>
  599. C3DFrameT<TULColor,TLRColor>*
  600. C3DFrameT<TULColor,TLRColor>::CreateFromStream(
  601.     LStream *inStream)
  602. {
  603.     return (new C3DFrameT(inStream));
  604. }
  605.  
  606. // ---------------------------------------------------------------------------
  607. //        • C3DFrameT
  608. // ---------------------------------------------------------------------------
  609. //    The default constructor does nothing.
  610.  
  611. template <RGBColor TULColor, RGBColor TLRColor>
  612. C3DFrameT<TULColor,TLRColor>::C3DFrameT()
  613. {
  614. }
  615.  
  616. // ---------------------------------------------------------------------------
  617. //        • C3DFrameT
  618. // ---------------------------------------------------------------------------
  619. //    The construct-from-stream constructor just constructs the base class.
  620.  
  621. template <RGBColor TULColor, RGBColor TLRColor>
  622. C3DFrameT<TULColor,TLRColor>::C3DFrameT(LStream *inStream)
  623.     : LPane(inStream)
  624. {
  625. }
  626.  
  627. // ---------------------------------------------------------------------------
  628. //        • DrawSelf
  629. // ---------------------------------------------------------------------------
  630.  
  631. template <RGBColor TULColor, RGBColor TLRColor>
  632. void
  633. C3DFrameT<TULColor,TLRColor>::DrawSelf()
  634. {
  635.     Rect         theRect;
  636.     
  637.     CalcPortFrameRect(theRect);
  638.  
  639.     U3DDrawingUtils::Draw3DRect( &theRect, &TULColor, &TLRColor);
  640. }
  641.  
  642.  
  643. //#endif    // __Templates